package test;

import java.io.IOError;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

import src.GenericService;
import src.NIOWebServer;

public class BootStrap {
    public static void main(String[] args) {
        BootStrap bootStrap = new BootStrap();
        // bootStrap.testScannerInput();
        bootStrap.testReadFile();
        //bootStrap.testDynamicLoadController();
    }

    /**
     * 1.Java阻塞式IO Scanner示例
     */
    public void testScannerInput() {
        Scanner inputScanner = new Scanner(System.in);
        try {
            System.out.println("请输入:");
            if (inputScanner.hasNextLine()) {
                String line = inputScanner.nextLine();
                System.out.printf("接收到用户输入:%s", line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inputScanner.close();
        }
    }

    public String testReadFile() {
        // NIOWebServer.WorkerConfigure config = new NIOWebServer.WorkerConfigure(10,
        // 15, 1000, 800);
        String rawContent = NIOWebServer.readContentFromDisk("resources/nio.html");
        System.out.printf("原始文件内容:%s\n", rawContent);
        return rawContent;
    }

    public void testDynamicLoadController() {
        // 相当于用一个结构体指针读取
        GenericService controller = null;
        String classPath = "src.MetricService";
        if (controller == null) {
            // 反射机制加载类，生成动态内容
            try {
                Class<GenericService> genericSerClass = (Class<GenericService>) Class.forName(classPath);
                controller = genericSerClass.newInstance();
            } catch (Exception e) {
                System.err.printf("Controller类加载异常:%s\n", e.getMessage());
                e.printStackTrace();
            }
        }
    }

}